//
//  DataGenerator.m
//  Pocket Doctor alpha version
//
//  Created by Sherif Nosseir on 11-12-22.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
//known bugs:
//some medicines in database have '?' in their names - ? should be replaced with "i" - example: 02237179

#import "AppDelegate.h"
#import "DataGenerator.h"

@implementation DataGenerator
@synthesize aD;

#define WORD_LENGTH 5

static NSString *letters = @"abcdefghijklmnopqrstuvwxyz";

/*- (NSString *)LoadDinNumber{
    
    NSString *loadDIN = [[NSString alloc]initWithFormat:@"SELECT desc FROM dinlist"];
    
    sqlite3_stmt * statement;
    const char * query_stmt;
    
    query_stmt = [loadDIN UTF8String];
    if (sqlite3_prepare_v2(dinDB, query_stmt, -1, &statement, NULL) == SQLITE_OK){
        
        if (sqlite3_step(statement) == SQLITE_ROW){
            NSString *medicineName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
            medicineName = [[NSString alloc]initWithFormat:@"%@%@",[medicineName substringToIndex:1],[[medicineName substringFromIndex:1]lowercaseString]];
            
            return loadDIN;
        }
        else{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Invalid DIN" message:@"DIN cannot be found in database." delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
            
            [alert show];
            [alert release];
            
            return nil;
        }
    }
    return nil;
}
*/

//Get All medications from database
- (NSArray *)wordsFromLetters{
    
/*    aD = [[UIApplication sharedApplication] delegate];
    
    NSString *dinDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"din.db"];
    sqlite3_open([dinDBPath UTF8String], &dinDB);

    NSString *dinDB = [self LoadDinNumber];*/
    
    NSMutableArray *content = [NSMutableArray new];
    
    NSString *dinDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"din.db"];
    sqlite3_open([dinDBPath UTF8String], &dinDB);
    
    sqlite3_stmt * statement;
    const char * query_stmt;
    
    //change to desc instead of din when database is fixed.
    NSString *loadDIN = [[NSString alloc]initWithFormat:@"SELECT din FROM dinlist;"];
    
    query_stmt = [loadDIN UTF8String];
    if (sqlite3_prepare_v2(dinDB, query_stmt, -1, &statement, NULL) == SQLITE_OK){
        
        while (sqlite3_step(statement) == SQLITE_ROW){
            
            NSMutableDictionary *row = [[[NSMutableDictionary alloc] init] autorelease];
            NSMutableArray *words = [[[NSMutableArray alloc] init] autorelease];
        
            NSString *currentWord = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
            
            currentWord = [currentWord capitalizedString];
            
            [words addObject:currentWord];
            
            NSString *currentLetter = [currentWord substringToIndex:1];
            [row setValue:[NSString stringWithString:currentLetter] forKey:@"headerTitle"];
            [row setValue:words forKey:@"rowValues"];
            [content addObject:row];
            }
            
            return content; 
            
            /*
            NSMutableDictionary *row = [[[NSMutableDictionary alloc] init] autorelease];
            NSMutableArray *words = [[[NSMutableArray alloc] init] autorelease];
            
            NSString *currentWord = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
            
            currentWord = [currentWord uppercaseString];
            
            [words addObject:currentWord];
            
            char currentLetter[2] = { toupper([letters characterAtIndex:0]), '\0'};
            [row setValue:[NSString stringWithCString:currentLetter encoding:NSASCIIStringEncoding]
                   forKey:@"headerTitle"];
            [row setValue:words forKey:@"rowValues"];
            [content addObject:row];*/
    }
    
    return content; 
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
